package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.DrawableUtil;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.ImageBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.di.component.DaggerVerifyCodeComponent;
import com.finace.money.di.module.VerifyCodeModule;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.presenter.VerifyCodePresenter;
import com.finace.money.presenter.contract.VerifyCodeContract;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/12/8.
 */

public class LoginActivity extends BaseActivity<VerifyCodePresenter> implements View.OnClickListener
        , VerifyCodeContract.View {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.et_money_account)
    EditText etMoneyAccount;
    @BindView(R.id.et_number_person)
    EditText etNumberPerson;
    @BindView(R.id.et_number_card_accept)
    EditText etNumberCardAccept;
    @BindView(R.id.checkbox_login_tip)
    CheckBox checkboxLoginTip;
    @BindView(R.id.tv_forget_passward)
    TextView tvForgetPassward;
    @BindView(R.id.button_login)
    Button buttonLogin;
    @BindView(R.id.tv_open_account_mine)
    TextView tvOpenAccountMine;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.iv_image_code)
    ImageView ivImageCode;

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void init() {

        rlLeft.setOnClickListener(this);
        ivImageCode.setOnClickListener(this);
        buttonLogin.setOnClickListener(this);
        tvOpenAccountMine.setOnClickListener(this);
        mPresenter.getImageCode();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_LOGIN);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
        String remeber = ACache.get(UIUtil.getContext()).getAsString(Constant.remeber);
        if(StringUtil.isNotNullString(remeber) && "1".equals(remeber)){
            checkboxLoginTip.setChecked(true);
            String account = ACache.get(UIUtil.getContext()).getAsString(Constant.remeber_account);
            etMoneyAccount.setText(account);
        }else {
            ACache.get(UIUtil.getContext()).remove(Constant.remeber_account);
            checkboxLoginTip.setChecked(false);
        }
    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //查询资金
            MessageSendUtil.QryAccount();
            //查询委托
            MessageSendUtil.QryOrder();
            //查询成交
            MessageSendUtil.QryTrade();
            //查询持仓
            MessageSendUtil.QryPosition();
            //是否绑定过银行卡
            MessageSendUtil.QryInvestor();

            if (null != intent) {
                ACache.get(UIUtil.getContext()).put(Constant.remeber,checkboxLoginTip.isChecked()?"1":"0");
                ACache.get(UIUtil.getContext()).put(Constant.remeber_account,etMoneyAccount.getText().toString());
                showLoading(false);
                goHome(0);
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerVerifyCodeComponent.builder().appComponent(appComponent)
                .verifyCodeModule(new VerifyCodeModule(this))
                .build().inject(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_left:
                onBackPressed();
                break;

            case R.id.iv_image_code:
                mPresenter.getImageCode();
                break;

            case R.id.button_login:
                String account = etMoneyAccount.getText().toString().trim();
                String password = etNumberPerson.getText().toString().trim();
                String imageCode = etNumberCardAccept.getText().toString().trim();

                /*if(StringUtil.isNullString(account)){
                    UIUtil.showToastSafe(UIUtil.getString(R.string.input_money_account));
                    return;
                }
                if(StringUtil.isNullString(password)){
                    UIUtil.showToastSafe(UIUtil.getString(R.string.input_passward_deal));
                    return;
                }
                if(StringUtil.isNullString(imageCode)){
                    UIUtil.showToastSafe(UIUtil.getString(R.string.input_check_code_image));
                    return;
                }*/

//                MessageSendUtil.LoginRequest(account,password);
                MessageSendUtil.LoginRequest("1880003","123456");
                showLoading(true);
                break;
            case R.id.tv_open_account_mine:
                startActivity(new Intent(UIUtil.getContext(), OpenAccountActivity.class));
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        goHome(0);
    }

    @Override
    public void sendSucceed() {

    }

    @Override
    public void getImageSucess(ImageBean imageBean) {
        ivImageCode.setImageBitmap(DrawableUtil.stringtoBitmap(imageBean.getImg_code_url()));
    }
}
